package com.ossjk.qlh.system.controller;

import java.io.Serializable;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.RequestBean;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.loginfo.LogModule;
import com.ossjk.qlh.loginfo.LogOperation;
import com.ossjk.qlh.system.entity.Dictionary;
import com.ossjk.qlh.system.service.IDictionaryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@LogModule(value = "数据字典管理")
@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/system/dictionary")
public class DictionaryController extends BaseController {
	@Autowired
	private IDictionaryService iDictionaryService;

	@LogOperation("列表")
	@ApiOperation(value = "列表")
	@PostMapping(value = "/list")
	@RequiresPermissions("00000010")
	public ResponseBean<Page<Dictionary>> list(@RequestBody RequestBean requestBean) {
		QueryWrapper<Dictionary> queryWrapper = new QueryWrapper();
		// "类型 1-业务配置 2-系统配置
		queryWrapper.eq("type", 1).orderByDesc("crtm");
		// 名称
		if (!CommonUtil.isBlank(requestBean.getParam().get("name"))) {
			queryWrapper.like("name", requestBean.getParam().get("name"));
		}
		// 键
		if (!CommonUtil.isBlank(requestBean.getParam().get("dkey"))) {
			queryWrapper.like("dkey", requestBean.getParam().get("dkey"));
		}
		return responseSuccess(iDictionaryService.page(requestBean.getPage(), queryWrapper));
	}

	@LogOperation("去编辑")
	@ApiOperation(value = "去编辑")
	@PostMapping(value = "/toUpdate")
	@RequiresPermissions("000000100005")
	public ResponseBean<Dictionary> toUpdate(@RequestBody RequestBean requestBean) {
		Dictionary dictionary = iDictionaryService.getById((Serializable) requestBean.getParam().get("id"));
		if (!CommonUtil.isBlank(dictionary)) {
			return responseSuccess(dictionary);
		} else {
			return responseFail();
		}
	}

	@LogOperation("添加")
	@ApiOperation(value = "添加")
	@PostMapping(value = "/save")
	@RequiresPermissions("000000100000")
	public ResponseBean save(@RequestBody RequestBean<Dictionary> requestBean) {
		Dictionary dictionary = requestBean.getData();
		// 查找所有的dkey
		List<Dictionary> dictionaries = iDictionaryService.list();
		// dkey不能重复
		for (Dictionary d : dictionaries) {
			if (CommonUtil.isEquals(d.getDkey(), dictionary.getDkey())) {
				return responseFail("dkey重复！");
			}
		}
		dictionary.setType(1);
		if (iDictionaryService.save(dictionary)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("编辑")
	@ApiOperation(value = "编辑") 
	@PostMapping(value = "/update")
	@RequiresPermissions("000000100005")
	public ResponseBean update(@RequestBody RequestBean<Dictionary> requestBean) {
		Dictionary dictionary = requestBean.getData();
		if (iDictionaryService.updateById(dictionary)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@LogOperation("删除")
	@ApiOperation(value = "删除")
	@PostMapping(value = "/remove")
	@RequiresPermissions("000000100010")
	public ResponseBean remove(@RequestBody RequestBean requestBean) {
		List ids = (List) requestBean.getParam().get("ids");
		if (iDictionaryService.removeByIds(ids)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
